package com.controller;

import com.entity.News;
import com.util.DataSourceUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.lang.annotation.ElementType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;

@WebServlet("/filter/updateNews")
public class UpdateNewsServlet extends HttpServlet
{
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.getRequestDispatcher("/WEB-INF/jsp/xianshi.jsp").forward(req,resp);
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
    {
        String nid=req.getParameter("nid");
        News news=new News();
        String sql="select * from Newss where nid=?";
        try(Connection con= DataSourceUtils.getConnection();
            PreparedStatement ps=con.prepareStatement(sql))
        {
            ps.setString(1,req.getParameter("nid"));
            try(ResultSet rs=ps.executeQuery()){
                rs.next();
                news.setNid(rs.getString("nid"));
                news.setTitle(rs.getString("Title"));
                news.setAuthor(rs.getString("Author"));
                news.setDateTime(rs.getObject("PublishDate", LocalDateTime.class));
                news.setContent(rs.getString("Content"));
                news.setImageurl(rs.getString("ImageURL"));
            }
        }
        catch (SQLException e) {
            throw new RuntimeException(e);
        }

        String  newauthor=req.getParameter("newauthor");
        String newtitle=req.getParameter("newtitle");
        String newcontent=req.getParameter("newcontent");
        String newimageurl=req.getParameter("newimageurl");
        String dateTimeStr = req.getParameter("newdateTime");
        LocalDateTime newdateTime= null;
        if (dateTimeStr!= null&& !dateTimeStr.trim().isEmpty()) {
            newdateTime=LocalDateTime.parse(dateTimeStr);
        }
        String sql2="update Newss set Title = ?,Author=?,PublishDate=?,Content=?,ImageURL=? where nid = ?";
        try (Connection con= DataSourceUtils.getConnection(); PreparedStatement ps = con.prepareStatement(sql2) ){
            ps.setString(1, (newtitle != "") ? newtitle : news.getTitle());
            ps.setString(2, (newauthor != "") ? newauthor : news.getAuthor());
            ps.setObject(3, newdateTime != null ? newdateTime : news.getDateTime());
            ps.setString(4, (newcontent != "") ? newcontent : news.getContent());
            ps.setString(5, (newimageurl != "") ? newimageurl : news.getImageurl());
            ps.setString(6, nid);
            ps.executeUpdate();
            }catch (SQLException e) {
            throw new RuntimeException(e);
        }
        resp.sendRedirect(req.getContextPath()+"/filter/houtai");
    }
}
